package com.zyiot.utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.TranslateAnimation;

import com.zyiot.adapter.EshopAdapter;

public class ZyUnit {

	/**
	 * 移动方法
	 * 
	 * @param v
	 *            需要移动的View
	 * @param startX
	 *            起始x坐标
	 * @param toX
	 *            终止x坐标
	 * @param startY
	 *            起始y坐标
	 * @param toY
	 *            终止y坐标
	 */
	public static void moveFrontBg(View v, int startX, int toX, int startY, int toY) {
		TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY);
		anim.setDuration(200);
		anim.setFillAfter(true);
		v.startAnimation(anim);
	}

	/**
	 * 根据网络图片存储成bitmap格式
	 * 
	 * @param view
	 * @return
	 */
	public static Bitmap getBitmapFromUrl(String imgUrl) {
		URL url;
		Bitmap bitmap = null;
		try {
			url = new URL(imgUrl);
			InputStream is = url.openConnection().getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			bitmap = BitmapFactory.decodeStream(bis);
			bis.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	private static boolean IsBusy = false;
	private static int retry = 3;

	public static boolean BitmapFromUrl(final Handler handler, final String imgUrl) {
		if (imgUrl == null)
			return false;

		new Thread() {
			@Override
			public void run() {
				int count = retry;
				while (count > 0) {
					URL url;
					Bitmap bitmap = null;
					try {
						url = new URL(imgUrl);

						InputStream is = url.openConnection().getInputStream();
						BufferedInputStream bis = new BufferedInputStream(is);
						bitmap = BitmapFactory.decodeStream(bis);
						if (bitmap != null) {
							Message msgTmp = handler.obtainMessage(EshopAdapter.MSG_NET_SUCC);
							msgTmp.obj = bitmap;
							msgTmp.sendToTarget();
							bis.close();
							break;
						}

					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					count--;
				}
				if (count <= 0 && handler != null) {
					Message msgTmp = handler.obtainMessage(EshopAdapter.MSG_NET_TIMEOUT);
					msgTmp.sendToTarget();
				}
			}
		}.start();
		return true;

	}

	/**
	 * 从view 得到图片
	 * 
	 * @param view
	 * @return
	 */
	public static Bitmap getBitmapFromView(View view) {
		view.destroyDrawingCache();
		view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
				View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		view.setDrawingCacheEnabled(true);
		Bitmap bitmap = view.getDrawingCache(true);
		return bitmap;
	}

	public static double String2double(String str) {
		try {
			double value = Double.valueOf(str);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static String[] spiltArray(String[] arrays, String splitChar, int index) {

		String[] result = new String[arrays.length];
		for (int i = 0; i < arrays.length; i++) {
			String[] temp = arrays[i].split(splitChar);
			result[i] = temp[index].toString();
		}
		return result;
	}

	public static String[] spiltStr(String Str, String splitChar) {
		String[] temp = Str.split(splitChar);
		return temp;
	}

}
